package com.sinodata.bsm.cicp.util.pool;

import org.apache.commons.pool.PoolableObjectFactory;

import com.sinodata.bsm.cicp.util.telnet.EthzSSH;

/**
 * 
 * <p>
 * Description: 
 * </p>
 *
 * @author wangyazhou
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-10-23 下午3:52:44          wangyazhou        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class SSH2PoolableObject implements PoolableObjectFactory {

    private String ip;

    private String user;

    private String pwd;

    private int port = 22;

    /**
     * @param ip
     * @param user
     * @param pwd
     */
    public SSH2PoolableObject(String ip, int port, String user, String pwd) {
        this.ip = ip;
        this.user = user;
        this.pwd = pwd;
        this.port = port;
    }

    @Override
    public void activateObject(Object arg0) throws Exception {

    }

    @Override
    public void destroyObject(Object obj) throws Exception {
        EthzSSH ethzssh = (EthzSSH) obj;
        //        System.out.println(" 销毁一个对象  ");
        ethzssh.disconn();
    }

    @Override
    public Object makeObject() throws Exception {
        //        System.out.println("新建一个连接...");
        EthzSSH ethzssh = new EthzSSH();
        ethzssh.login(ip, port, user, pwd);
        ethzssh.setValid(true);
        return ethzssh;
    }

    @Override
    public void passivateObject(Object obj) throws Exception {

    }

    @Override
    public boolean validateObject(Object obj) {
        EthzSSH ethzssh = (EthzSSH) obj;
        boolean b = ethzssh.getValid();
        return b;
    }

}
